package cn.openea.perpartner.meily.web.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.openea.perpartner.common.core.utils.BeanUtils;
import cn.openea.perpartner.meily.util.UserContext;
import cn.openea.perpartner.meily.web.entity.Category;
import cn.openea.perpartner.meily.web.entity.dto.CategoryDto;
import cn.openea.perpartner.meily.web.mapper.CategoryMapper;
import cn.openea.perpartner.meily.web.service.ICategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Category)表服务实现类
 *
 * @author Weird077
 * @since 2023-01-11 21:04:18
 */
@Slf4j
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Override
    public boolean importData(List<Category> list) {
        List<String> dataList = new ArrayList<>();
        for (Category category : list) {
            dataList.add(category.getIndustry() + "#" + category.getClassify() + "#" + category.getCategory());
        }
        HashMap<String, String> industryMap = new HashMap<>();
        for (String data : dataList) {
            String[] arr = data.split("#");
            industryMap.put(arr[0], "root");
        }
        List<Category> industryList = new ArrayList<>();
        for (Map.Entry<String, String> entry : industryMap.entrySet()) {
            Category category = new Category();
            category.setCategory(entry.getKey());
            category.setParentId(entry.getValue());
            category.setId(IdUtil.simpleUUID());
            category.setCreateUser(UserContext.getUserId());
            category.setUpdateUser(UserContext.getUserId());
            industryList.add(category);
        }
        List<Category> classifyList = new ArrayList<>();
        for (String data : dataList) {
            String[] arr = data.split("#");
            for (Category industry : industryList) {
                if (arr[0].equals(industry.getCategory())) {
                    boolean isExist = false;
                    for (Category classify : classifyList) {
                        if (classify.getCategory().equals(arr[1])
                                && classify.getParentId().equals(industry.getId())) {
                            isExist = true;
                            break;
                        }
                    }
                    if (isExist) {
                        break;
                    }
                    Category category = new Category();
                    category.setCategory(arr[1]);
                    category.setParentId(industry.getId());
                    category.setId(IdUtil.simpleUUID());
                    category.setCreateUser(UserContext.getUserId());
                    category.setUpdateUser(UserContext.getUserId());
                    classifyList.add(category);
                }
            }
        }
        List<Category> categoryList = new ArrayList<>();
        for (String data : dataList) {
            String[] arr = data.split("#");
            for (Category industry : industryList) {
                for (Category classify : classifyList) {
                    if (arr[0].equals(industry.getCategory()) && arr[1].equals(classify.getCategory())
                            && industry.getId().equals(classify.getParentId())
                    ) {
                        Category category = new Category();
                        category.setCategory(arr[2]);
                        category.setParentId(classify.getId());
                        category.setId(IdUtil.simpleUUID());
                        category.setCreateUser(UserContext.getUserId());
                        category.setUpdateUser(UserContext.getUserId());
                        categoryList.add(category);
                    }
                }

            }
        }
        list.clear();
        list.addAll(industryList);
        list.addAll(classifyList);
        list.addAll(categoryList);
        return saveBatch(list);
    }



    @Override
    public List<CategoryDto> selectList(String parentId) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isBlank(parentId)) {
            parentId = "root";
        }
        wrapper.eq(Category::getParentId, parentId);
        wrapper.orderByDesc(Category::getCategory);
        List<Category> list = list(wrapper);
        if (!list.isEmpty()) {
            return BeanUtils.copyListProperties(list, CategoryDto::new);
        }
        return new ArrayList<>();
    }
}

